import unittest
import requests
from api.openaccount_api import OpenAccountApi
from api.regloginapi import RegLoginApi
from parameterized import parameterized
from common.read_json import read_json
from common.utils import common_assert
from common.html_uilt import html_util



class TestOpenAccount(unittest.TestCase):

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        # 封装类的实例化
        self.open_account_api = OpenAccountApi(self.session)
        self.reg_login_api = RegLoginApi(self.session)

    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()

    # 实名认证测试用例
    @parameterized.expand(read_json("/open_account","realname"))
    def test01_realname(self,keywords,password,realname,card_id,exp_status_code,exp_status,description,check):
        # 实现登录接口
        response = self.reg_login_api.user_login(keywords, password)
        body = {'realname': realname, 'card_id ': card_id}
        # 实现实名认证接口
        response = self.open_account_api.realname(body)
        json_data = response.json()
        # 断言
        # 基础断言
        common_assert(self, response, exp_status_code, exp_status, description)
        # 实名信息断言
        if check == 0:
            self.assertEqual("楚**",json_data['data']['realname'])
            self.assertEqual("450****798",json_data['data']['card_id'])

    # 开户测试用例
    def test02_open_account(self):
        # 实现登录接口
        response = self.reg_login_api.user_login("15878945622", "l123456")
        # 实现开户接口
        response = self.open_account_api.open_account()
        json_data = response.json()

        # 断言
        self.assertEqual(200,response.status_code)
        self.assertEqual(200, json_data['status'])
        self.assertIn("form",json_data['description'])


    # 第三方开户测试用例
    def test03_third_open_account(self):
        # 实现登录接口
        response_l = self.reg_login_api.user_login("15878945622", "l123456")
        # 实现开户接口
        response_o = self.open_account_api.open_account()

        # 实现第三方开户接口
        url = html_util(response_o)[0]
        form_data = html_util(response_o)[1]
        print(url)
        print(form_data)
        response_third = self.open_account_api.third_open_account(url,form_data)
        # 断言
        self.assertIn('OK',response_third.text)
